package com.scanner;

import java.io.IOException;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

@SuppressLint("NewApi")
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    public Camera mCamera;
	public JniWrapper jniWrapper;
	private boolean torchOn = false;
    
    public native int[] calculatePoints(byte[] imageData, int height, int width);
        
    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        
        WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        
        Camera.Parameters cParam = mCamera.getParameters();

        cParam.setPictureSize(width, height);
        cParam.setPreviewSize(width, height);
        
        cParam.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        
        mCamera.setParameters(cParam);
        
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
    }
    public void ToggleFlash()
    {
    	Camera.Parameters cParam = mCamera.getParameters();
    	if (torchOn)
    	{
    		cParam.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);    		
    	}
    	else
    	{
    		cParam.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    	}
    	torchOn = !torchOn;
    	mCamera.setParameters(cParam);    	
    }
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d("CameraView", "Error setting camera preview: " + e.getMessage());
        }
     }
    public void surfaceDestroyed(SurfaceHolder holder) {
       // empty. Take care of releasing the Camera preview in your activity.
    }
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.
        if (mHolder.getSurface() == null){
             // preview surface does not exist
             return;
        }
        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview
        }
        // set preview size and make any resize, rotate or
        // reformatting changes here
        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
       } catch (Exception e){
           Log.d("CameraView", "Error starting camera preview: " + e.getMessage());
       }
    }
    
    public void onPause() {
        mCamera.release();
        mCamera = null;
    }
    
    public void ScanPicture(ShutterCallback shutter, PictureCallback picture)
    {
    	if (picture == null)
    	{
    		Log.d("myError", "picture callback problem");
    		return;
    	}
    	mCamera.takePicture(shutter, null, picture);
    }
}